// index.js
// 获取应用实例
const app = getApp()

Page({
  onLoad() {
    this.ctx = wx.createCameraContext()
  },
  data: {
    classifyResult: "识别结果将显示在此处",
    src: null,
    inputValue: ""
  },
  bindKeyInput: function (e) {
    this.setData({
      inputValue: e.detail.value
    })
  },
  manulCheck(){
    if(this.data.src){
      wx.uploadFile({
        url: 'http://49.233.9.115/manual_classify', 
        filePath: this.data.src,
        name: 'uploadfile_ant',
        formData: {
          'hint':this.data.inputValue
        },
        success: (res)=>{
          console.log('upload success')
          let data = res.data
          let parsed_data = JSON.parse(data)
          console.log(data)
          this.setData({
            classifyResult:parsed_data['result']
          })
          //do something
        },
        fail:()=>{
          console.log('failed')
        }
      })
    }
  },
  takePhoto(){
    this.ctx.takePhoto({
      quality: 'high',
      success: (res) => {
        this.setData({
          src: res.tempImagePath
        });
        console.log('photo taken')
        //console.log('called')
        //console.log(res.tempImagePath)
        //启动上传等待中...
        wx.showToast({
          title: '正在上传...',
          icon: 'loading',
          mask: true,
          duration: 5000
        })
        wx.uploadFile({
          url: 'http://49.233.9.115/photo', 
          filePath: res.tempImagePath,
          name: 'uploadfile_ant',
          formData: {
          },
          success: (res)=>{
            console.log('upload success')
            let data = res.data
            let parsed_data = JSON.parse(data)
            console.log(data)
            this.setData({
              classifyResult:parsed_data['result'],
              disabled:false
            })
          },
          fail:()=>{
            console.log('failed')
          }
        })//上传图片并获取识别结果
      }
    })
  }
})
